home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD ROM Paradise Collection 4
/
CD ROM Paradise Collection 4 1995 Nov.iso
/
program
/
swagd_f.zip
/
DATETIME.SWG
/
0031_UNIXDATE Routines.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-11-21
|
4KB
|
108 lines
(* A public domain Turbo Pascal unit to convert between the date formats *)
(* of DOS and Unix; by Robert Walking-Owl October 1993 *)
unit UnixDate;
interface
function DosToUnixDate(DOSTime: LongInt): LongInt;
function UnixToDosDate(UnixDate: LongInt): LongInt;
implementation
uses DOS;
function DosToUnixDate(DOSTime: LongInt): LongInt;
const DaysInMonth: array[1..12] of word =
(30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var i, j : Word;
UnixDate: LongInt;
DTR: DateTime;
begin
UnPackTime(DOSTime,DTR);
UnixDate := 0;
UnixDate:=(DTR.year-1970)*365+((DTR.year-1971) div 4);
j:=pred(DTR.day);
if DTR.month<>1
then for i:=1 to pred(DTR.month) do j:=j+DaysInMonth[i];
if ((DTR.year mod 4)=0) and (DTR.month>2)
then inc(j);
UnixDate:=UnixDate+j; (* Add number of days this year *)
UnixDate:=(UnixDate*24)+DTR.hour;
UnixDate:=(UnixDate*60)+DTR.min;
UnixDate:=(UnixDate*60)+DTR.sec;
DosToUnixDate:=UnixDate;
end;
function UnixToDosDate(UnixDate: LongInt): LongInt;
const DaysInMonth: array[1..12] of word =
(30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var i, j : Word;
DTR: DateTime;
DosTime: LongInt;
begin
DaysInMonth[2]:=28;
DTR.sec := UnixDate mod 60; UnixDate := UnixDate div 60;
DTR.min := UnixDate mod 60; UnixDate := UnixDate div 60;
DTR.hour := UnixDate mod 24; UnixDate := UnixDate div 24;
DTR.day := UnixDate mod 365; UnixDate := UnixDate div 365;
DTR.year := UnixDate+1970;
DTR.day := 1+DTR.day-((DTR.year-1972) div 4);
if (DTR.day > (31+29)) and ((DTR.year mod 4)=0)
then inc(DaysInMonth[2]);
DTR.month:=1;
while DTR.day>DaysInMonth[DTR.Month]
do begin
DTR.day := DTR.day - DaysInMonth[DTR.Month];
inc(DTR.month)
end;
PackTime(DTR,DosTime);
UnixToDosDate:=DosTime;
end;
end.
This archive includes the Turbo Pascal source for a unit that will
convert between Unix-file timestamps and DOS-file timestamps.
The advantage is that you can write software, such as archivers
or archiver-utilities which can handle Unix-style dates and times.
(Note many systems will store the data in BigEndian format, so
you'll have to do a further bit of conversion.)
If the value is bigendian: Turbo Pascal includes the function Swap
for words. To swap a long integer you'll have to reverse the bytes.
Both systems store a packed record of the date and time in a four
byte long-integer (also called a double-word).
DOS stores the date and time (of a file) actually as two packed words:
Date: Time:
Bit: FEDCBA98 76543210 FEDCBA98 76543210
xxxxxxx. ........ ........ ........ Year - 1980
.......x xxx..... ........ ........ Month (1-12)
........ ...xxxxx ........ ........ Day (1-31)
........ ........ xxxxx... ........ Hours (0-23)
........ ........ .....xxx xxx..... Minutes (0-59)
........ ........ ........ ...xxxxx Seconds/2 (0-29)
Unix stores the date as the number of seconds since January 1, 1970 UTC
(Universal Coordinated Time = Grenwich Mean Time). The is an _exact_
number (not including leap seconds)--it accounts for months of 28 (or
29, for leap years), 30 and 31 days.
Note that some (Unix) software assumes your time is set for UTC and stores
the date/time stamp blindly, while others attempt to figure out which
time zone you're in and convert the time appropriately. (This can be
done if the TZ variable is set properly.) So don't fret if you find the
conversions a few hours off...